package org.dromara.system.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.utils.RandomUtils;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.AppWarehouseOutVo;
import org.dromara.system.domain.bo.AppWarehouseOutBo;
import org.dromara.system.service.IAppWarehouseOutService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 出库单
 *
 * @author Lion Li
 * @date 2025-10-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/warehouseOut")
public class AppWarehouseOutController extends BaseController {

    private final IAppWarehouseOutService appWarehouseOutService;




    /**
     * 仓库管理员 上传货接收单
     */
    @SaCheckPermission("system:driverDetail:list")
    @GetMapping("/uploadReceivedOrder")
    @RepeatSubmit()
    public R<Void> uploadReceivedOrder(AppWarehouseOutBo bo) {
        return appWarehouseOutService.uploadReceivedOrder(bo);
    }



    /**
     * 新增出库单
     */
    @SaCheckPermission("system:warehouseOut:add")
    @Log(title = "出库单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppWarehouseOutBo bo) {
        return toAjax(appWarehouseOutService.insertByBo(bo));
    }



    /**
     * 生成出库单id
     */
    @SaCheckPermission("system:warehouseOut:add")
    @Log(title = "出库单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @GetMapping("/genId")
    public R<String> genId() {
        return R.ok("CK" + RandomUtils.generateId());
    }

    /**
     * 查询出库单列表
     *   /**
     *    type --> 0 待出库 1已出库 2已退货
     **/
    @SaCheckPermission("system:warehouseOut:list")
    @GetMapping("/list")
    public TableDataInfo<AppWarehouseOutVo> list(AppWarehouseOutBo bo, PageQuery pageQuery) {
        return appWarehouseOutService.queryPageList(bo, pageQuery);
    }


    /**
     * 退货
     **/
    @SaCheckPermission("system:warehouseOut:list")
    @GetMapping("/saleReturn")
    public R<?> saleReturn(@Param("outOrderNo") String outOrderNo) {
        return appWarehouseOutService.saleReturn(outOrderNo);
    }


    /**
     * 导出出库单列表
     */
    @SaCheckPermission("system:warehouseOut:export")
    @Log(title = "出库单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppWarehouseOutBo bo, HttpServletResponse response) {
        List<AppWarehouseOutVo> list = appWarehouseOutService.queryList(bo);
        ExcelUtil.exportExcel(list, "出库单", AppWarehouseOutVo.class, response);
    }

    /**
     * 获取出库单详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:warehouseOut:query")
    @GetMapping("/{id}")
    public R<AppWarehouseOutVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(appWarehouseOutService.queryById(id));
    }


    /**
     * 修改出库单
     */
    @SaCheckPermission("system:warehouseOut:edit")
    @Log(title = "出库单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppWarehouseOutBo bo) {
        return toAjax(appWarehouseOutService.updateByBo(bo));
    }

    /**
     * 删除出库单
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:warehouseOut:remove")
    @Log(title = "出库单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(appWarehouseOutService.deleteWithValidByIds(List.of(ids), true));
    }
}
